#include "player.h"

Player::Player(AnimationManager &a, Level& lvl, int X, int Y): miscellanea(a, X, Y){
    option("player",0,100,"idle");
    state = 1;
    run = jump = 0;
    dx = dy = 0;
    hit = false;
    st = false;
    devil = false;
    gg = false;
    obj = lvl.GetAllObjects();
}

void Player::keyCheck(){
    if(key["Down"]){
        if (!devil) pray = true;
        trypray = true;
    }
    if(key["Space"]){
        attack = true;
    }
    if(key["R"] && !key["Up"]){ 
        dir = false;
        if (onGround) {
            run = 1;
            state = 0;
        }
        dx = 0.3;
    }
    if(key["L"] && !key["Up"]){
        dir = true;
        if (onGround) {
            run = 1;
            state = 0;
        }
        dx = -0.3;
    }
    if(key["Up"]){
        dx = 0;
        
        if(key["L"]){
            dir = true;
            dx = - 0.3;
            
        }
        if(key["R"]){
            dir = false;
            dx = 0.3;
            
        }
        if (onGround){
            dy = -0.5;
            state = 0;
            jump = 1;
            anim.play("jump");
        }
    }
    if(!(key["R"] || key["L"]) && !key["Up"]){
        dx = 0;
        if(run == 1){
            run = 0;
            state = 1;
        }
        if(jump == 1){
            jump = 0;
            state = 1;
        }
    }
    if(!key["Down"]){
        pray = false;
        trypray = false;
        state = 1;
    }
    if(!key["Space"]){
        attack = false;
    }
}

void Player::update(float time){
    keyCheck();
    if (devil){
        if (state == 1) anim.set("idled");
        if (jump == 1) anim.set("jumpd");
        if (run == 1) anim.set("rund");
        if (trypray) anim.set("throwd");
        if (attack) anim.set("attackd");
        if (hit) {
            anim.set("deathd");
            st = true;
        } 
        if (gg){
            anim.set("pushd");
            st = true;
        }
        if (dir) anim.flip();
    }

    else{
        if (state == 1) anim.set("idle");
        if (jump == 1) anim.set("jump");
        if (run == 1) anim.set("run");
        if (pray) anim.set("throw");
        if (attack) anim.set("attack");
        if (hit) {
            anim.set("death");
            st = true;
        }
        if (gg){
            anim.set("push");
            st = true;
        } 
        if (dir) anim.flip();
    }

    x += dx * time;

    Collision(0);

    if (!onGround) dy = dy + 0.001 * time;
    y += dy * time;
    onGround = false;

    Collision(1);
    anim.tick(time, st);
    
    key["R"] = key["L"] = key["Up"] = key["Down"] = false;
}

sf::FloatRect Player::getRect(){
    return sf::FloatRect(x, y, anim.getW(), anim.getH());
}

void Player::Collision(int situation){  
    for (int i=0; i<obj.size(); i++){
        if (getRect().intersects(obj[i].rect)){
            if (obj[i].name == "solid"){
                if (dy > 0 && situation == 1){ 
                    y = obj[i].rect.top - anim.getH();  
                    dy=0; 
                    onGround = true;  
                    state = 1;
                }
                if (dy < 0 && situation == 1){ 
                    y = obj[i].rect.top + obj[i].rect.height ;   
                    dy=0;
                }
                if (dx > 0 && situation == 0){ 
                    x = obj[i].rect.left - anim.getW(); 
                }
                if (dx < 0 && situation == 0){ 
                    x = obj[i].rect.left + obj[i].rect.width ;
                }
            }
            if (obj[i].name == "peaks"){
                if (dy > 0 && situation == 1){ 
                    y = obj[i].rect.top - anim.getH();  
                    dy=0; 
                    onGround = true;  
                    state = 1;
                }
                if (dy < 0 && situation == 1){ 
                    y = obj[i].rect.top + obj[i].rect.height ;   
                    dy=0;
                }
                if (dx > 0 && situation == 0){ 
                    x = obj[i].rect.left - anim.getW(); 
                }
                if (dx < 0 && situation == 0){ 
                    x = obj[i].rect.left + obj[i].rect.width ;
                }
                hit = true;
                life = false;
            }
            if (obj[i].name == "carrot"){
                // if (dy > 0 && situation == 1){ 
                //     y = obj[i].rect.top - anim.getH();  
                //     dy=0; 
                //     onGround = true;
                // }
                // if (dy < 0 && situation == 1){ 
                //     y = obj[i].rect.top + obj[i].rect.height ;   
                //     dy=0;
                // }
                // if (dx > 0 && situation == 0){ 
                //     x = obj[i].rect.left - anim.getW(); 
                // }
                // if (dx < 0 && situation == 0){ 
                //     x = obj[i].rect.left + obj[i].rect.width ;
                // }
                if (!gg) gg = true;

            }
        }
    }
}
void Player::draw(sf::RenderWindow &window){
    anim.draw(window, x, y + anim.getH());
}